package com.edu.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.dao.BaseDao;
import com.edu.dao.UserDao;
import com.edu.dao.Impl.UserDaoImpl;
import com.edu.entity.User;
import com.edu.service.UserService;
import com.edu.service.Impl.UserServiceImpl;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		User user = new User();//创建User对象
		String username = request.getParameter("username");//获取用户名
		String userpass = request.getParameter("userpass");//获取密码
		String sex = request.getParameter("sex");//获取性别
		String phone = request.getParameter("phone");//获取电话号码
		user.setUsername(username);//设置用户名
		user.setUserpass(userpass);//设置密码
		user.setSex(sex);//设置性别
		user.setPhone(phone);//设置电话号码
		System.out.println(user);
		
		UserDao userDao = new UserDaoImpl();//创建UserDaoImpl对象
		int row = userDao.add(user);//调用UserDaoImpl中的register（）方法进行注册，返回受影响行数
		System.out.println(row);
		
		if (row == 1) {//如果注册成功，则重定向到登录页面
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}else {//否则重定向到注册页面
			System.out.println("错误");
			response.sendRedirect("register.jsp");
		}
	}

}
